package me.leiusual.sharing.ui.mine

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations.switchMap
import androidx.lifecycle.ViewModel
import me.leiusual.sharing.api.model.Address
import me.leiusual.sharing.repository.AddressRepository
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.util.extension.toLiveData
import javax.inject.Inject

/**
 * author: leiusual
 * create on: 2019-07-28
 * description:
 */
class MineAddressViewModel @Inject constructor(
    private val addressRepository: AddressRepository
) : ViewModel() {

  // 列表

  private val _addressList = MutableLiveData<Boolean>()
  val addressListFirst: LiveData<Boolean> = _addressList
  val addressList: LiveData<Data<List<Address>>> = switchMap(_addressList) {
    addressRepository.addressList().toLiveData()
  }

  fun addressList(isFirst: Boolean) {
    _addressList.value = isFirst
  }

  // 删除

  private val _addressDelete = MutableLiveData<Long>()
  val addressDelete: LiveData<Data<Unit>> = switchMap(_addressDelete) {
    addressRepository.addressDelete(it).toLiveData()
  }

  fun addressDelete(aId: Long) {
    _addressDelete.value = aId
  }
}